package com.lagou;

import com.lagou.bean.ComsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AnnotationConsumerMain {
    public static void main(String[] args) throws Exception {

        System.out.println("-------------");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        // 获取消费者组件
        ComsumerComponent service = context.getBean(ComsumerComponent.class);

        ExecutorService executor = Executors.newFixedThreadPool(30);

        // 连接注册中心成功后在执行后续
        boolean f = true;
        while (f) {
            System.in.read();
            f = false;
        }

        while (true) {
            Task task1 = new Task("task1", service);
            Task task2 = new Task("task2", service);
            Task task3 = new Task("task3", service);
            executor.execute(task1);
            executor.execute(task2);
            executor.execute(task3);

            // 控制一分钟触发2000次
            Thread.sleep(30);
        }

    }


    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }

    static class Task implements Runnable {

        private String name;

        private ComsumerComponent comsumerComponent;

        public Task(String taskName, ComsumerComponent comsumerComponent) {
            name = taskName;
            this.comsumerComponent = comsumerComponent;
        }

        @Override
        public void run() {
            if ("task1".equals(name)) {
                comsumerComponent.sayHello1("task1");
            } else if ("task2".equals(name)) {
                comsumerComponent.sayHello2("task2");
            } else if ("task3".equals(name)) {
                comsumerComponent.sayHello3("task3");
            }
        }
    }
}
